define(['jquery', 'underscore', 'backbone', 'bootstrap_switch',
// Main template for this view
	'text!views/$!{moduleName}/main.html',
	'text!views/$!{moduleName}/update.html',
	'dialog'], function ($, _, Backbone,
						 bootstrap_switch, mainTemplate, updateTemplate, dialog) {

	var ModuleNode = Backbone.Model.extend({
		defaults: {
			idAttribute: "#foreach($!{primaryKey} in $!{introspectedTable.primaryKeyColumns})$!{primaryKey.javaProperty}#end",
			#foreach($!{item} in $!{fields})
			$!{item.javaProperty}:'',
            #end
		},
		urlRoot: "$!{moduleName}/update"
	});
	var ResultCollection = Backbone.Collection.extend({
		models: ModuleNode,
		url: '/$!{moduleName}/page/1/10',
		initialize: function () { // Deferred load
		}
	});
	function serializeObject(form){
		var o ={};
		$.each(form.serializeArray(),function(index){
			if(o[this['name']]){
				o[this['name']] = o[this['name']] +","+this['value'];
			}else{
				o[this['name']] = this['value'];
			}
		});
		return o;
	}
	var MainView = Backbone.View.extend({
		el: $('#main-content'),
		initialize: function () {
			this.collection = new ResultCollection();
			//this.collection = new ModuleNodeList();
			this.collection.unbind('add');
			this.collection.unbind('reset');

			this.collection.unbind('all');
			//this.collection.bind('add', this.addOne, this);
			this.collection.bind('reset', this.reload, this);

			//this.collection.bind('all', this.render, this);

			this.table = $('.table');


		},
		events: {
			'click .$!{moduleName}-container .btn_add_channel': 'add_module',
			'submit .$!{moduleName}-container form': 'form',
			'click .$!{moduleName}-container .btn_query': 'query',
			'click .$!{moduleName}-container .btn_update': 'btn_update',
			'click .$!{moduleName}-container .btn_delete': 'btn_delete'
		},
		addOne: function (todo) {
			//view = new ResultView({
			//	model : todo
			//});
			//this.$(".table").append(view.render().el);
		},

		reload: function () {

		},
		btn_update : function() {
			var _this = this;
			var data = {};
			dialog.dialog({
				template: updateTemplate,
				collection: this.collection,
				url: '$!{moduleName}/query/' + $(event.target).parents('tr').attr('$!{introspectedTable.primaryKeyColumns[0].javaProperty}'),
				afterOpen: function () {
					$('.make-switch').bootstrapSwitch();
				},
				title: "编辑模块",
				className: "update_module",
				save: function () {
					var switch1 = $('.update_module input[name=effective]').bootstrapSwitch('state');
					var module = $.extend(new ModuleNode(), {attributes : serializeObject($(this).find("form"))});
					module.idAttribute = 'moduleId';
					module.urlRoot = "$!{moduleName}/update";
					//_this.model.id = $('input[name=id]').val();
					var a = module.save(null, {
						success: function () {
							window.mainView.reload();
						}
					});
					return a;
				}
			});
		},
		query: function () {
			this.render({ name : $('input[name=bundleIdSearch]').val()});
		},
		form: function () {
			return;
		},
		add_dialog: function (data) {
			var html = $(_.template(addTemplate, data));
			html.find('.make-switch').bootstrapSwitch();
			return html;
		},
		add_module: function (e) {
			var thiz = this;
			var data = {};
			dialog.dialog({
				title: "aaaa",
				html: thiz.add_dialog(data),
				buttons: {
					cancel: {
						label: "取消"
					},
					save: {
						label: "保存",
						className: "blue",
						callback: function () {
							var model = new ModuleNode({
								id: null,
								code: $('input[name=name]').val(),
								name: $('input[name=name]').val(),
								link: $('input[name=link]').val(),
								target: $('input[name=target]').val(),
								orderIndex: $('input[name=target]').val(),
								isOuter: 0,
								parentId: $('input[name=parentId]').val(),
								effective: $('input[name=effectiveString]')
									.val() == 'on' ? 1 : 0,
							});
							var a = model.save(null, {
								success: function () {
									window.mainView.reload();
								}
							});
						}
					}
				}
			});
		},
		//render: function (param) {
		//	param = param || $(this).find("form");
		//	this.collection.url = '/$!{moduleName}/page/1/10?' + $.param(param);
		//	var _this = this;
		//	_this.table.empty();
		//	this.collection.fetch({
		//		success: function (collection, resp) {
		//			var data = {};
		//			data.page = collection.models[0];
		//			#set($Var="_this.$"+"el.html(_.template(mainTemplate, data));")
		//			$Var
		//			$('.make-switch').bootstrapSwitch();
		//		}
		//	});
        //
		//}
		render: function (param) {
			param = param || $(this).find("form");
			//this.collection.url = '/icon/search/1/10?' + $.param(param);
			var _this = this;
			_this.table.empty();
			this.collection.fetch({
				contentType : 'application/json',
				url : '/$!{moduleName}/search/1/10',
				type : 'POST',
				data : JSON.stringify(param),
				success: function (collection, resp) {
					var data = {};
					data.page = collection.models[0];
					#set($Var="_this.$"+"el.html(_.template(mainTemplate, data));");
					$Var
					$('.make-switch').bootstrapSwitch();
				}
			});

		}
	});
	window.mainView = new MainView();
	return window.mainView;
});
